<?php
App::uses('AppController', 'Controller');
/**
 * BenhhaiTrieuchungs Controller
 *
 * @property BenhhaiTrieuchung $BenhhaiTrieuchung
 * @property PaginatorComponent $Paginator
 */
class BenhhaiTrieuchungsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
	   $this->loadModel('Benhhai');
       $benhhais = $this->Benhhai->find('list',array('fields'=>array('id','ten_benh')));
       
       $this->set(compact('benhhais'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->BenhhaiTrieuchung->exists($id)) {
			throw new NotFoundException(__('Invalid benhhai trieuchugn'));
		}
		$options = array('conditions' => array('BenhhaiTrieuchung.' . $this->BenhhaiTrieuchung->primaryKey => $id));
		$this->set('BenhhaiTrieuchung', $this->BenhhaiTrieuchung->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add($id=null) {
	    $result =false;
       	if (!empty($this->request->data)) {
       	    $datas = $this->request->data['BenhhaiTrieuchung'];
            
       	    foreach($datas as $k=>$v)
            {
                if(!empty($v['ma_trieuchung'])){
                    $this->BenhhaiTrieuchung->create();
                    if ($this->BenhhaiTrieuchung->save($v)) {
        				$result = true;
        			} else {
        		      $result =false;
                   	}
                }
			
            }
            if($result)
            {
                $this->Session->setFlash(__('The benhhai trieuchugn has been saved'));
				return $this->redirect(array('action' => 'admin_index'));
            }
            
		}
        $this->loadModel('Bophan');
        $bophans=$this->Bophan->find('all');
        $this->set(compact('bophans','id'));
        
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->BenhhaiTrieuchung->exists($id)) {
			throw new NotFoundException(__('Invalid benhhai trieuchugn'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->BenhhaiTrieuchung->save($this->request->data)) {
				$this->Session->setFlash(__('The benhhai trieuchugn has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The benhhai trieuchugn could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('BenhhaiTrieuchung.' . $this->BenhhaiTrieuchung->primaryKey => $id));
			$this->request->data = $this->BenhhaiTrieuchung->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->BenhhaiTrieuchung->id = $id;
		if (!$this->BenhhaiTrieuchung->exists()) {
			throw new NotFoundException(__('Invalid benhhai trieuchugn'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->BenhhaiTrieuchung->delete()) {
			$this->Session->setFlash(__('The benhhai trieuchugn has been deleted.'));
		} else {
			$this->Session->setFlash(__('The benhhai trieuchugn could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
    public function listTrieuchung($benhId)
    {
        $this->layout=false;
        $this->BenhhaiTrieuchung->recursive=2;
        $list = $this->BenhhaiTrieuchung->find('all',array('conditions'=>array('BenhhaiTrieuchung.ma_benhhai'=>$benhId)));
        //debug($list);
        $this->set(compact('list'));
    }
 }
